--[[
Copyright 2010, 2011 João Cardoso
Scrap Visualizer is distributed under the terms of the GNU General Public License (or the Lesser GPL).
This file is part of Scrap Visualizer.

Scrap Visualizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Scrap Visualizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Scrap Visualizer. If not, see <http://www.gnu.org/licenses/>.
--]]

local Visualizer = ScrapVisualizer
local L = Scrap_Locals
local List = {}


--[[ Startup ]]--

function Visualizer:Startup()
	ScrapVisualizerCloseButton:SetScript('OnClick', function() MerchantFrame:Hide() end)
	ScrapVisualizerTab2:SetText(L.NotJunk)
	ScrapVisualizerTab1:SetText(L.Junk)
	
	PanelTemplates_TabResize(ScrapVisualizerTab1, 0)
	PanelTemplates_TabResize(ScrapVisualizerTab2, 0)
	PanelTemplates_SetNumTabs(self, 2)
	PanelTemplates_SetTab(self, 1)

	self.button:Show()
	self.loading:SetText(L.Loading)
	self.TitleText:SetText(L.ScrapList)
	self.portrait:SetTexture('Interface\\Addons\\Scrap\\Art\\Enabled Icon')
	self:SetFrameLevel(self:GetFrameLevel() + 3)
	self:SetScript('OnUpdate', self.QueryItems)
	self:SetPoint('BOTTOMLEFT', 13, 61)
	self:SetPoint('TOPRIGHT', -34, -13)
	self:EnableMouse(true)
	self:UpdateButton()
	self:QueryItems()
	
	local portraitBack = self:CreateTexture(nil, 'BORDER')
	portraitBack:SetPoint('TOPRIGHT', self.portrait, -5, -5)
	portraitBack:SetPoint('BOTTOMLEFT', self.portrait, 5, 5)
	portraitBack:SetTexture(0, 0, 0)
	
	local UpdateTabs = MerchantFrame_Update
	function MerchantFrame_Update()
		if MerchantFrame.selectedTab ~= id then
			UpdateTabs()
			self:Hide()
		end
	end
end

function Visualizer:QueryItems()
	for itemID in pairs(Scrap_List) do
		if not GetItemInfo(itemID) then
			return
		end
	end
	
	HybridScrollFrame_CreateButtons(self.scroll, 'ScrapVisualizer_ButtonTemplate', 1, -2, 'TOPLEFT', 'TOPLEFT', 0, -TOKEN_BUTTON_OFFSET)
	hooksecurefunc('Scrap_Hooks', function() self:UpdateList() end)
	
	self.Startup, self.QueryItems = nil
	self:SetScript('OnUpdate', nil)
	self.loading:Hide()
	self:UpdateList()
end


--[[ Update ]]--

function Visualizer:UpdateList()
	if not self.QueryItems then
		wipe(List)
		
		local showJunk = self.selectedTab == 1
		for itemID in pairs(Scrap_List) do
			if Scrap:IsJunk(itemID) == showJunk then
				tinsert(List, itemID)
			end
		end
		
		sort(List, function(A, B)
			if not A then
				return true
			elseif not B or A == B then
				return nil
			end
			
			local nameA, _ , qualityA = GetItemInfo(A)
			local nameB, _ , qualityB = GetItemInfo(B)
			return qualityA > qualityB or (qualityA == qualityB and nameA < nameB)
		end)
		
		self.scroll:update()
	end
	
	self:UpdateButton()
end

function Visualizer:UpdateButton()
	local tab = self.selectedTab
	if tab == self.selectionType then
		self.button:Enable()
	else
		self.button:Disable()
	end
	
	self.button:SetText(tab == 1 and L.Remove or L.Add)
	self.button:SetWidth(self.button:GetTextWidth() + 20)
end

function Visualizer.scroll:update()
	local self = Visualizer.scroll
	local selection = Visualizer.selection
	local offset = HybridScrollFrame_GetOffset(self)
	local width = #List > 18 and 296 or 318
	local focus = GetMouseFocus()
	
	for i, button in pairs(self.buttons) do
		local index = i + offset
		local itemID = List[index]
		
		if itemID then
			local name, link, quality = GetItemInfo(itemID)
			button.text:SetTextColor(GetItemQualityColor(quality))
			button.icon:SetTexture(GetItemIcon(itemID))
			button.text:SetText(name)
			button:SetWidth(width)
			button.item = itemID
			button.link = link
			button:Show()
			
			if itemID == selection then
				button:LockHighlight()
			else
				button:UnlockHighlight()
			end
			
			if focus == button then
				button:GetScript('OnEnter')(button)
			end
			
			if mod(index, 2) == 0 then
				button.stripe:Show()
			else
				button.stripe:Hide()
			end
		else
			button:Hide()
		end
	end
	
	HybridScrollFrame_Update(self, #List * 20 + 2, #self.buttons * 18)
	self:SetWidth(width + 5)
end

Visualizer:Startup()